package com.example.stroebackstage.servlet;

import com.example.stroebackstage.entity.Administrator;
import com.example.stroebackstage.exception.LoginException;
import com.example.stroebackstage.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String name = req.getParameter("name");
       String password = req.getParameter("password");
       AdminService adminService = new AdminService();
       try{
            Administrator administrator = adminService.login(name,password);
            req.getSession().setAttribute("admin",administrator);
            resp.sendRedirect(req.getContextPath()+"/welcome.jsp");

        }catch(LoginException e){
            e.printStackTrace();
            req.setAttribute("login_massage",e.getMessage());
            req.getRequestDispatcher("/admin_login.jsp").forward(req,resp);
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
